package com.wzt.lianfirecontrol.fragment;

import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.wzt.lianfirecontrol.App;
import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.activity.BaseActivity;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.adapter.utils.BarTitleAdapter;
import com.wzt.lianfirecontrol.adapter.utils.FragmentPagerAdapter;
import com.wzt.lianfirecontrol.bean.ConstData;
import com.wzt.lianfirecontrol.bean.recode.BannerModel;
import com.wzt.lianfirecontrol.utils.ScreenUtils;
import com.wzt.lianfirecontrol.utils.SpUtils;
import com.wzt.lianfirecontrol.utils.Utils;
import com.wzt.lianfirecontrol.view.InitView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 故障与屏蔽
 */
public class BarTitleFragment extends BaseFragment {

    private BaseActivity activity;
    private Bundle bundle;
    private View itemContentView;

    private RelativeLayout rl_bar_title;
    private ImageView iv_back;
    private TextView tv_edit;
    private RecyclerView rlv_title_bar;
    private LinearLayoutManager linearLayoutManager;
    private BarTitleAdapter barTitleAdapter;

    private ViewPager viewPager;
    private FragmentPagerAdapter fragmentPagerAdapter;
    private List<Fragment> fragments;
    private int currentSelectPosition = -1;

    private View include_no_data;
    private View include_no_net;
    private View include_progress_bar;

    private static final int BAR_REQUEST_WHAT = 0;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (null == itemContentView) {
            bundle = getArguments();
            activity = getMyActivity();
            itemContentView = inflater.inflate(R.layout.f_bar_title, container, false);
            initView();
        }
        if (itemContentView.getParent() != null) {
            ((ViewGroup) itemContentView.getParent()).removeView(itemContentView);
        }
        return itemContentView;
    }

    private void initView() {
        initHeadView();
        initViewPager();

        include_progress_bar = itemContentView.findViewById(R.id.include_progress_bar);
        include_progress_bar.setVisibility(View.GONE);
        InitView.instance().initProgressBar(activity, include_progress_bar);

        include_no_data = itemContentView.findViewById(R.id.include_no_data);
        include_no_data.setVisibility(View.GONE);

        include_no_net = itemContentView.findViewById(R.id.include_no_net);
        include_no_net.setVisibility(View.GONE);
        include_no_net.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                updateData();
            }
        });

        List<BannerModel> bannerModels = new ArrayList<>();
        BannerModel bannerModel = new BannerModel();
        bannerModel.setTitle("故障");
        bannerModels.add(bannerModel);
        BannerModel bannerModel1 = new BannerModel();
        bannerModel1.setTitle("屏蔽");
        bannerModels.add(bannerModel1);
        setFragments(bannerModels);
    }

    private void initHeadView() {
        rl_bar_title = itemContentView.findViewById(R.id.rl_bar_title);
        iv_back = itemContentView.findViewById(R.id.iv_back);
        tv_edit = itemContentView.findViewById(R.id.tv_edit);
        rlv_title_bar = itemContentView.findViewById(R.id.rlv_title_bar);
        linearLayoutManager = new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rlv_title_bar.setLayoutManager(linearLayoutManager);
        barTitleAdapter = new BarTitleAdapter(activity);
        rlv_title_bar.setAdapter(barTitleAdapter);
        barTitleAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<BannerModel>() {
            @Override
            public void onItemClick(View v, int position, BannerModel data) {
                viewPager.setCurrentItem(position);
            }
        });

        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) rl_bar_title.getLayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            layoutParams.height = Utils.dip2px(activity, 50) + ScreenUtils.getStatusHeight(activity);
            rl_bar_title.setPadding(0, ScreenUtils.getStatusHeight(activity), 0, 0);
        }
    }

    private void initViewPager() {
        fragments = new ArrayList<>();
        viewPager = itemContentView.findViewById(R.id.viewPager);
        viewPager.setOffscreenPageLimit(1);
        fragmentPagerAdapter = new FragmentPagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(fragmentPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                currentSelectPosition = position;
                if (position > 2) {
                    linearLayoutManager.scrollToPositionWithOffset(position - 2, 0);
                } else {
                    linearLayoutManager.scrollToPosition(0);
                }
                barTitleAdapter.setCurrentPosition(position);

                final Bundle skipBundle = new Bundle();
                HashMap<String, String> hashMap = new HashMap<>();
                final BannerModel bannerModel = new BannerModel();
                bannerModel.setTitle("历史");
                bannerModel.setActionType(ConstData.SHOWFRAG);
                bannerModel.setActionValue(ConstData.INFOLIST);
                if (0 == position) {
                    bannerModel.setActionTitle("故障历史");
                    hashMap.put("runStatus", Utils.changeStrToStatusId("故障"));
                    SpUtils.putString(App.context, "runStatus", "2");
                } else {
                    bannerModel.setActionTitle("屏蔽历史");
                    hashMap.put("runStatus", Utils.changeStrToStatusId("屏蔽"));
                    SpUtils.putString(App.context, "runStatus", "6");
                }
                bannerModel.setActionParams(hashMap);
                skipBundle.putBoolean("isHistory", true);
                skipBundle.putString(ConstData.SHOWTYPPE, ConstData.HOME_BAOJING_DEVICE_LIST);
                tv_edit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (currentSelectPosition == 1) {
                            skipBundle.putBoolean("isPingBi", true);

                        }
                        activity.setClickAction(bannerModel, skipBundle);
                    }
                });
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setFragments(List<BannerModel> bannerModels) {
        if (null != bannerModels && bannerModels.size() > 0) {
            include_no_data.setVisibility(View.GONE);
            fragments.clear();
            barTitleAdapter.clear();
            for (BannerModel bannerModel : bannerModels) {
                fragments.add(initFragment(bannerModel));
            }
            barTitleAdapter.addDatas(bannerModels);
            fragmentPagerAdapter.appendList(fragments);
            viewPager.setCurrentItem(0);
            currentSelectPosition = 0;
            barTitleAdapter.setCurrentPosition(0);


            final Bundle skipBundle = new Bundle();
            HashMap<String, String> hashMap = new HashMap<>();
            final BannerModel bannerModel = new BannerModel();
            bannerModel.setTitle("历史");
            bannerModel.setActionType(ConstData.SHOWFRAG);
            bannerModel.setActionValue(ConstData.INFOLIST);
            bannerModel.setActionTitle("故障历史");
            hashMap.put("runStatus", Utils.changeStrToStatusId("故障"));
            bannerModel.setActionParams(hashMap);
            skipBundle.putBoolean("isHistory", true);
            skipBundle.putString(ConstData.SHOWTYPPE, ConstData.HOME_BAOJING_DEVICE_LIST);
            tv_edit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    activity.setClickAction(bannerModel, skipBundle);
                    SpUtils.putString(App.context, "runStatus", "2");
                }
            });
        } else {
            include_no_data.setVisibility(View.VISIBLE);
        }
    }

    private Fragment initFragment(BannerModel bannerModel) {
        BaseFragment baseFragment = new InfoListFragment();
        Bundle bundle = new Bundle();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("runStatus", Utils.changeStrToStatusId(bannerModel.getTitle()));
        hashMap.put(ConstData.SHOWTYPPE, ConstData.HOME_BAOJING_DEVICE_LIST);
        bundle.putBoolean("isSelectTitle", true);
        bundle.putSerializable(ConstData.ACTIONPARAMS, hashMap);
        bundle.putSerializable(ConstData.ACTIONMODLE, bannerModel);
        baseFragment.setArguments(bundle);
        return baseFragment;
    }

}
